<?php
session_start();
require_once('../includes/config.php');
require_once('includes/dbfunctions.php');

if (!isset($_SESSION['User'])) {
    require ("login.php");
    exit;
}

if (isset($_POST['cancel'])) {
    require ("index.php");
    exit;
}

$newPageTitle = $_POST['pagetitle'];
$alias = $_POST['pageurl'];
$pageContent = $_POST['editor'];
$pageDesc = $_POST['pagedesc'];

$oldPageInfo = getPageInfo($alias);


// If old page does not exists, create new page
if ($oldPageInfo === NULL) {
    $result = addNewContent($alias, $newPageTitle, $_SESSION['User'], $pageContent, $pagedesc, $contentRoot);
    if ($result === TRUE) {
        header("Location: index.php?msg=pagecreated");
    } else {
        header("Location: index.php?msg=errcreating");
    }
} else {

    if ($oldPageInfo['Approve'] == 0 || trim($pageInfo['LastFilename']) != "") {
        header("Location: index.php?msg=waitingapproval");
    }
    
    $result = editContent($alias, $newPageTitle, $_SESSION['User'], $pageContent, $desc, $contentRoot);
    if ($result === TRUE) {
        header("Location: index.php?msg=pagecreated");
    } else {
        header("Location: index.php?msg=errcreating");
    }
}
?>

